package com.examplemartin.jpademo.service;

import com.examplemartin.jpademo.dao.SupplierDao;
import com.examplemartin.jpademo.pojo.po.supplier.SupplierPO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SupplierService {

    @Resource
    private SupplierDao supplierDao;

    // 返回供应商列表
    public List<SupplierPO> findAll() {
        return supplierDao.findAll();
    }
    // 返回供应商列表 条件为 status = 1
    public List<SupplierPO> findAllByStatus(Integer status) {
        return supplierDao.findAllByStatus(status);
    }

    // 按id检索
    public SupplierPO findById(Long id) {
        SupplierPO supplierPO = supplierDao.findById(id).get();
        return supplierPO;
    }

    // 按供应商名称检索 -- 用于 01 添加供应商时 检测是否重名
    public List<SupplierPO> findAllByName(String name) {
        return supplierDao.findAllByName(name);
    }


    // 保存供应商
    public SupplierPO save(SupplierPO supplierPO) {
        return supplierDao.save(supplierPO);
    }

    // 计算总条目
    public long countAll() {
        long count = supplierDao.count();
        return count;
    }

    // 查询分页
    public List<SupplierPO> findAllPage(Pageable page) {
        if (page == null) {
            page = PageRequest.of(0, 10);
        }

        Page<SupplierPO> all = supplierDao.findAll(page);
        List<SupplierPO> content = all.getContent();
        return content;
    }


}
